import Vue from 'vue'
import Router from 'vue-router'
import homeRouter from './home-router'

// 按需（懒）加载
const LayoutIndex = () => import(/* webpackChunkName: "LayoutHome" */ '@/layouts/index')
Vue.use(Router)

const base = `${process.env.BASE_URL}` // 动态获取二级目录
const router = new Router({
  mode: 'hash',
  base: base,
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import(/* webpackChunkName: "Login" */ '@/views/login')
    },
    {
      path: '/index',
      name: 'LayoutIndex',
      component: LayoutIndex,
      children: [
        ...homeRouter
      ]
    }
  ],
  scrollBehavior (to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return {
        x: 0,
        y: 0
      }
    }
  }
})

router.beforeEach((to, from, next) => {
  next()
})

export default router
